package com.bn.feeds.tools;

	import java.util.ArrayList;
	import java.util.List;
	import java.util.Map;

import com.bn.feedMaster.FeedProcessException;
import com.bn.feeds.MasterFeedConfiguration;

	import atg.core.util.StringUtils;
	import atg.repository.MutableRepository;
	import atg.repository.MutableRepositoryItem;
	import atg.repository.Repository;
	import atg.repository.RepositoryException;
import atg.repository.RepositoryItem;

	/**
	 * The Class StoreLocatorRepositoryTools.
	 * @author Vinay
	 */
	public class StoreLocatorRepositoryTools extends BaseRepositoryTools {

		/** The repository. */
		private Repository repository;

		/**
		 * Adds/updates a repository item with the data provided
		 *
		 * @param config
		 *            configuration for the Feed
		 * @param itemList
		 *            one Row data
		 * @param header
		 *            the feed file header
		 * @param idColumnIndex
		 *            index of id column property in data row(itemList)
		 * @param repositoryPropertiesToTokenIndexMap
		 *            a map which contains repository properties(primitive type) index in data row(which property has value at what index in data row)
		 * @param repositoryItemPropertiesToTokenIndexMap
		 *            a map which contains repository properties(RepositoryItem type) index in data row(which property has value at what index in data row)
		 * @param productProcessedMap
		 *            a map which contains id's of all the items which are processed so far in the feed execution
		 * @return
		 * @throws FeedProcessException
		 *             the feed process exception
		 */
		public List<String> processRecord(MasterFeedConfiguration config, String[] dataRow, String[] header, int idColumnIndex, Map<String, Integer> repositoryPropertiesToTokenIndexMap, Map<String, Integer> repositoryItemPropertiesToTokenIndexMap,
	            Map<String, String> productProcessedMap, Map<String, Integer> mandatoryPropertiesMap) throws FeedProcessException {
	        if (isLoggingDebug()) {
	            logDebug("StoreLocatorRepositoryTools::>processRecord()::BEGIN");
	        }
	        List<String> errorList = new ArrayList<String>();
	        if(!validateMandatoryProperties(config, mandatoryPropertiesMap, dataRow, errorList)){
	            return errorList;
	            }
	        MutableRepository mutableRespository = config.getRepository();
	        Repository repository = config.getPrdRepository();
	        String itemDescriptorName = config.getItemDescriptorName();
	        String itemId = dataRow[idColumnIndex];
	        if (!StringUtils.isBlank(itemId)) {
	            try {
	            	itemId = itemId.trim();
	                RepositoryItem item = null;
	                MutableRepositoryItem mutableItem = null;

	                item = repository.getItem(itemId, itemDescriptorName);
	                if (item != null) {
	                    if (isLoggingDebug()) {
	                        logDebug("StoreLocatorRepositoryTools::>processRecord()::Department item with id:" + item.getRepositoryId() + " already Exists. Updating Item");
	                    }
	                    mutableItem = mutableRespository.getItemForUpdate(itemId, itemDescriptorName);
	                    setPropertiesToItem(mutableRespository, repository, mutableItem, repositoryPropertiesToTokenIndexMap, repositoryItemPropertiesToTokenIndexMap, dataRow, itemDescriptorName, errorList);
	                    mutableRespository.updateItem(mutableItem);
	                } else {
	                    if (isLoggingDebug()) {
	                        logDebug("StoreLocatorRepositoryTools::>processRecord()::Department item with id:" + itemId + " does not Exists. Creating new Item");
	                    }
	                    mutableItem = mutableRespository.createItem(itemId, itemDescriptorName);
	                    setPropertiesToItem(mutableRespository, repository, mutableItem, repositoryPropertiesToTokenIndexMap, repositoryItemPropertiesToTokenIndexMap, dataRow, itemDescriptorName, errorList);
	                    mutableRespository.addItem(mutableItem);
	                }
	            } catch (RepositoryException e) {
	            	logError(e);
	                throw new FeedProcessException(e);
	            } catch (NumberFormatException e) {
	            	logError(e);
	                throw new FeedProcessException(e);
	            }
	        } else {

	            if (isLoggingDebug()) {
	                logDebug("StoreLocatorRepositoryTools::>processRecord()::Value of Id property in feed is null");
	            }
	            throw new FeedProcessException("Value of Id property in feed is null");

	        }
	        if (isLoggingDebug()) {
	            logDebug("StoreLocatorRepositoryTools::>processRecord()::END");
	        }
	        return errorList;
	    }

		/**
		 * Gets the repository.
		 *
		 * @return the repository
		 */
		public Repository getRepository() {
			return repository;
		}

		/**
		 * Sets the repository.
		 *
		 * @param repository
		 *            the new repository
		 */
		public void setRepository(Repository repository) {
			this.repository = repository;
		}


		@Override
		public boolean cleanUpData(MutableRepository repository,
				String itemDescriptor) throws FeedProcessException {
			// TODO Auto-generated method stub
			return false;
		}
	}
